package com.mkl.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mkl.business.domain.Customer;
import com.mkl.business.domain.Goods;
import com.mkl.business.domain.Sales;
import com.mkl.business.mapper.SalesMapper;
import com.mkl.business.service.CustomerService;
import com.mkl.business.service.GoodsService;
import com.mkl.business.service.SalesService;
import com.mkl.business.vo.SalesVo;
import com.mkl.system.common.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * @author king
 * @version 1.0
 * @date 2020/4/8 11:57
 */
@Service
public class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements SalesService {
    @Autowired
    private SalesMapper salesMapper;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private GoodsService goodsService;

    @Override
    public DataGridView queryAllSales(SalesVo salesVo) {

        IPage<Sales> page = new Page<>(salesVo.getPage(), salesVo.getLimit());

        QueryWrapper<Sales> qw = new QueryWrapper<>();

        qw.eq(salesVo.getGoodsid() != null, "goodsid", salesVo.getGoodsid());
        qw.eq(salesVo.getCustomerid() != null, "customerid", salesVo.getCustomerid());

        qw.ge(salesVo.getStartTime() != null, "salestime", salesVo.getStartTime());
        qw.le(salesVo.getEndTime() != null, "salestime", salesVo.getEndTime());

        qw.orderByDesc("salestime");

        salesMapper.selectPage(page, qw);
        List<Sales> records = page.getRecords();
        for (Sales record : records) {
            if (null != record.getGoodsid()) {
                Goods goods = goodsService.getById(record.getGoodsid());
                record.setGoodsname(goods.getGoodsname());
                record.setSize(goods.getSize());
            }
            if (null != record.getCustomerid()) {
                Customer customer = customerService.getById(record.getCustomerid());
                record.setCustomername(customer.getCustomername());
            }
        }
        return new DataGridView(page.getTotal(), records);
    }

    @Override
    public Sales saveSales(Sales sales) {
        salesMapper.insert(sales);
        //更新库存
        Integer goodsId = sales.getGoodsid();
        Goods goods = goodsService.getById(goodsId);
        goods.setNumber(goods.getNumber() - sales.getNumber());
        goodsService.updateGoods(goods);
        return sales;
    }

    @Override
    public Sales updateSales(Sales sales) {
        Sales oldObj = salesMapper.selectById(sales.getId());
        Goods goods = goodsService.getById(oldObj.getGoodsid());
        goods.setNumber(goods.getNumber() + oldObj.getNumber() - sales.getNumber());
        goodsService.updateGoods(goods);
        salesMapper.updateById(sales);
        return sales;
    }

    @Override
    public Integer getAllAvailableSalesSum() {
        QueryWrapper<Sales> qw = new QueryWrapper<>();
        List<Sales> sales = salesMapper.selectList(qw);
        Integer sum = 0;
        for (Sales sale : sales) {

            sum += sale.getNumber();
        }
        return sum;
    }

    @Override
    public boolean removeById(Serializable id) {
        Sales sales = salesMapper.selectById(id);
        Goods goods = goodsService.getById(sales.getGoodsid());
        goods.setNumber(goods.getNumber() + sales.getNumber());
        goodsService.updateGoods(goods);
        return super.removeById(id);
    }
}
